package work.chenbo.zmqc.business.channe.dao.mongo;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.result.DeleteResult;
import lombok.RequiredArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import work.chenbo.zmqc.business.channe.model.ChannelGlobalRuleVO;

/**
 * @author ChenBo
 * @className ChannelGlobalRuleMongo
 * @date 2020/11/9
 */
@Service
@RequiredArgsConstructor
public class ChannelGlobalRuleMongo {
    private final MongoTemplate mongoTemplate;


    /**
     * 添加
     * @author ChenBo
     * @date 2020/11/9
     */
    public ChannelGlobalRuleVO save(ChannelGlobalRuleVO channelGlobalRuleVO){
        Integer roleId = channelGlobalRuleVO.getRoleId();
        Query query=new Query(Criteria.where("roleId").is(roleId));
        DeleteResult result = mongoTemplate.remove(query, ChannelGlobalRuleVO.class);
        System.out.println(JSONObject.toJSONString(result));
        ChannelGlobalRuleVO save = mongoTemplate.save(channelGlobalRuleVO);
        return save;
    }

    /**
     * 根据规则id查询
     * @author ChenBo
     * @date 2020/11/9
     */
    public ChannelGlobalRuleVO findByRoleId(Integer roleId){
        Query query=new Query(Criteria.where("roleId").is(roleId));
        return mongoTemplate.findOne(query , ChannelGlobalRuleVO.class);
    }

}
